package ae.pcfc.cldos.online.web.controller.tksadmin.all.request;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.support.SessionStatus;

import ae.pcfc.cldos.online.common.delegate.AdminTaskDelegate;
import ae.pcfc.cldos.online.common.delegate.CompanyInformationDelegate;
import ae.pcfc.cldos.online.common.exception.NoSuchCompanyException;
import ae.pcfc.cldos.online.common.vo.CompanyInformationVO;
import ae.pcfc.cldos.online.common.vo.StatusVO;
import ae.pcfc.cldos.online.common.vo.tasks.AdminTaskVO;
import ae.pcfc.cldos.online.common.vo.tasks.TaskRequestParamsVO;
import ae.pcfc.cldos.online.common.vo.tasks.TaskRequestVO;
import ae.pcfc.cldos.online.common.vo.tasks.TasksVO;
import ae.pcfc.cldos.online.web.ViewPath;
import ae.pcfc.cldos.online.web.controller.base.BasePageController;
import ae.pcfc.cldos.online.web.model.AdminTaskGridSearchBean;
import ae.pcfc.cldos.online.web.model.JQGridJSONModel;

@Controller
@RequestMapping(value = ViewPath.__TKSADMIN_ALL_APP_REQUESTS)
public class TksAdminAllRequestController extends BasePageController {

	@Autowired
	@Qualifier("adminTaskDelegate")
	private AdminTaskDelegate adminTaskDelegate;
	
	@Autowired
	@Qualifier("companyInformationDelegate")
	private CompanyInformationDelegate companyInformationDelegate;
	
	
	@RequestMapping(value = "/form.htm", method = RequestMethod.GET)
	public void login() {
		

	}
	/**
	 * @param request
	 * @return
	 */
	@ModelAttribute("StatusListAll")
	public List<StatusVO> getStatusListAll(HttpServletRequest request){
		List<StatusVO> statusList = adminTaskDelegate.findApplicationStatusByType("ALL");
		List<StatusVO> response = new ArrayList<StatusVO>();
		response.add(new StatusVO());
		response.addAll(statusList);
		return response;
	}
	/**
	 * @param model
	 */
	@ModelAttribute
	public void populateBreadCrumbs(ModelMap model) {
		breadCrumbsFactoryService.populateAllAppRequestsBreadCrumb(model);
	}
	
	/**
	 * @param request
	 * @param model
	 * @return
	 * @throws NoSuchCompanyException
	 */
	@ModelAttribute("companyList")
	public List<CompanyInformationVO> populateCompanyList(HttpServletRequest request,ModelMap model) throws NoSuchCompanyException{
		List<CompanyInformationVO> companyList = companyInformationDelegate.getAllActiveCompanies();
		return companyList;
	}
	
	@RequestMapping(value = "/show.htm", method = RequestMethod.GET)
	public void show() {
			
	}
	
	
	@RequestMapping(value = "/displayAllAppRequests")
	public @ResponseBody
	JQGridJSONModel displayAllAppRequests(HttpServletRequest request, 
			@ModelAttribute AdminTaskGridSearchBean adminTaskGridSearchBean,
			@RequestParam(value = "page", required = true) String page,
			@RequestParam(value = "rows", required = true) String rows,
			@RequestParam(value = "sidx", required = true) String sidx,
			@RequestParam(value = "sord", required = true, defaultValue = "asc") String sord,
			@RequestParam(value = "_search", required = false) String isSearch,
			@RequestParam(value = "searchField", required = false) String searchField,
			@RequestParam(value = "searchOper", required = false) String searchOper,
			@RequestParam(value = "searchString", required = false) String searchString) {

		JQGridJSONModel result = new JQGridJSONModel();

		Integer pageIndx = 1;
		Integer count = new Integer(20); //this.userDelegate.getUserList().size();
		Integer limit = 40;
		try {
			pageIndx = Integer.parseInt(page);
		} catch (Exception nullexp) {
			pageIndx = 1;
		}
		try {
			limit = Integer.parseInt(rows);
		} catch (Exception nullexp) {
			pageIndx = 10;
		}
		
		TaskRequestVO taskRequestVO = new TaskRequestVO();
		
		TaskRequestParamsVO taskRequestParamsVO = new TaskRequestParamsVO();
		mapper.map(adminTaskGridSearchBean, taskRequestParamsVO);
		taskRequestParamsVO.setCuurentPage(pageIndx);
		taskRequestParamsVO.setRowsPerPage(limit);
		if(!sord.equals("asc")){
			taskRequestParamsVO.setSortAsc(false);
		}
		
		taskRequestVO.setTaskRequestParamsVO(taskRequestParamsVO);
		
		TasksVO tasksVO = adminTaskDelegate.getAllAppRequestsForTksAdmin(taskRequestVO, adminTaskGridSearchBean.getTaskName());
		
		List<AdminTaskVO> adminTaskVOList = tasksVO.getAdminTaskVOs();

		Integer totalpages = 0;
		
		count = tasksVO.getTotal();
		
		if (count > 0)
			totalpages = (int) Math.ceil(((float) count) / (float) limit);
		else
			totalpages = 0;

		if (pageIndx > totalpages)
			pageIndx = totalpages;

		
		result.setPage(pageIndx.toString());
		result.setRecords(count + "");
		result.setTotal(totalpages + "");
		
		int i=0;
		Integer no=1;
		for(AdminTaskVO adminTaskVO : adminTaskVOList){
			List<String> cells = new ArrayList<String>();
			cells.add(no.toString());
			//cells.add(adminTaskVO.getTaskNameEn());
			cells.add(adminTaskVO.getServiceNameEn()); 
			cells.add(adminTaskVO.getReferenceNo());
			if(adminTaskVO.getDate()!=null){
				cells.add(adminTaskVO.getDate().toString());
			}else{
				cells.add("");
			}
			no++;

			cells.add(adminTaskVO.getStatus());
			cells.add("<a href='admin/general/review/show.htm?appId="+
			(encryptionDelegate.encryptAndEncode(adminTaskVO.getAppId().toString()))+"'><img src=\"resources/images/common/view_icon.jpg\" alt=\"open\"></a>");
			
			result.addRows((i++)+"", cells);
		}
		
		return result;
	}

	@RequestMapping(method = RequestMethod.POST, value = "/form.htm")
	public String form(
			HttpServletRequest request,
			SessionStatus status,
			BindingResult errors) throws Exception {
		
		return "" ;
	}
	
}
